<template>
  <el-dialog title="商品查询" :close-on-click-modal="false" :visible.sync="visible" top="-20px" fullscreen append-to-body center>
    <el-button class="miui-dialog-close-btn" @click="visible = false">关闭页面</el-button>
    <el-divider content-position="left">查询条件</el-divider>
    <div class="text item">
      <el-form inline size="mini">
        <el-form-item label="商品名称">
          <el-input v-model="dataForm.name"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-search" type="primary" @click="getDataList()">查询</el-button>
          <el-button icon="el-icon-delete" type="info" @click="clearSearchForm">清空</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-divider content-position="left">查询数据</el-divider>
    <div class="avue-crud__pagination" style="margin-bottom: 25px;">
      <el-button icon="el-icon-check" type="primary" @click="checkItemData" size="mini">选 择</el-button>
      <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pageIndex"
        :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" :total="totalPage" background layout="total, sizes, prev, pager, next">
      </el-pagination>
    </div>
    <el-table :data="dataList" height="calc(100vh - 215px)" ref="singleTable" highlight-current-row border v-loading="dataListLoading">
      <el-table-column key="1" type="index" header-align="center" align="center" width="50">
        <template slot-scope="scope">
          <el-button style="padding: 2px;" type="primary" size="mini" icon="el-icon-check" circle plain @click="handleCurrentChange(scope.row)"></el-button>
          <!-- <el-radio v-model="goodsId" :label="scope.row.id" style="position: relative;left: 6px;top:-2px;" @change="handleCurrentChange(scope.row)">&nbsp;</el-radio> -->
        </template>
      </el-table-column>
      <el-table-column key="2" prop="code" header-align="center" align="center" label="商品代码" v-if="dataForm.ctgId != '4'"></el-table-column>
      <el-table-column key="3" prop="brandName" header-align="center" align="center" label="商品品种" v-if="dataForm.ctgId != '4'"></el-table-column>
      <el-table-column key="4" prop="viewGoodsName" header-align="center" align="center" label="商品名称"></el-table-column>
      <el-table-column key="5" prop="customizeType" header-align="center" align="center" label="镜片型" v-if="dataForm.ctgId == '4'"></el-table-column>
      <el-table-column key="6" prop="goodsPrice" header-align="center" align="center" label="零售价格" v-if="dataForm.ctgId == '4'"></el-table-column>
    </el-table>
  </el-dialog>
</template>

<script>
  import { mapGetters } from 'vuex'
  import { filterParams, uniqObjArrById } from '@/util/tools'
  import { getGoodsPriceData } from '@/api/eims/goods'
  import { getGlassStorageSaleByPage } from "@/api/eims/storage";

  export default {
    data() {
      return {
        visible: false,
        dataForm: {
          ctgId: '',
          name: ''
        },
        goodsId: '',
        dataList: [],
        dataListLoading: false,
        pageIndex: 1,
        pageSize: 100,
        totalPage: 0,
      }
    },
    computed: {
      ...mapGetters(['bizData'])
    },
    methods: {
      init(data) {
        this.visible = true
        this.pageIndex = 1
        this.name = data.name
        this.index = data.index
        // this.dataForm = data.data
        this.dataForm = {}
        if(data.ctgId == '4'){
          if(data.name == 'suitableGlassOd'){
            this.dataForm.columnMirror = data.data.postGlassOd || 0
            this.dataForm.mirrorBall = data.data.ballGlassOd || 0
          }else if(data.name == 'suitableGlassOs'){
            this.dataForm.columnMirror = data.data.postGlassOs || 0
            this.dataForm.mirrorBall = data.data.ballGlassOs || 0
          }
          this.dataForm.customizeType = '0'
        }else{
          
        }
        this.dataForm.ctgId = data.ctgId
        this.getDataList()
      },
      //	清空查询表单
      clearSearchForm() {
        for (let key in this.dataForm) {
          if(key != 'ctgId'){
            this.dataForm[key] = ''
          }
        }
      },
      // 获取数据列表
      getDataList() {
        let params1 = { current: this.pageIndex, size: this.pageSize }
        let params2 = filterParams(this.dataForm)
        getGlassStorageSaleByPage(Object.assign(params1, params2)).then(result => {
          this.dataList = result.data.data.records
          this.totalPage = result.data.data.total
        })
      },
      // 每页数
      sizeChangeHandle(val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle(val) {
        this.pageIndex = val
        this.getDataList()
      },
      handleCurrentChange(val){
        let data = {
          name: this.name,
          index: this.index,
          data: val,
        }
        this.visible = false
        this.$emit('refreshData',data)
      },
      handleSelectionChange(val) {
        this.multipleSelection = uniqObjArrById(this.multipleSelection.concat(val))
      },
      //  选择并关闭
      checkItemData() {
        this.multipleSelection.forEach((item, index) => {
          if (this.formData.length) {
            for (let i = 0; i < this.formData.length; i++) {
              let idVal = (this.formData[i].goodsId) ? this.formData[i].goodsId : this.formData[i].id
              if (item.id == idVal) {
                item.quantity = this.formData[i].quantity
                item.applyQuantity = this.formData[i].applyQuantity
                item.memo = this.formData[i].memo
                if (item.ctgId == '4') {
                  item.invalidTimeStr = this.formData[i].invalidTimeStr
                  item.batch = this.formData[i].batch
                  item.registerNum = this.formData[i].registerNum
                }
              }
            }
          } else {
            item.quantity = ''
            item.applyQuantity = ''
            item.memo = ''
            if (item.ctgId == '4') {
              item.invalidTimeStr = ''
              item.batch = ''
              item.registerNum = ''
            }
          }
        })
        this.visible = false
        this.$emit('updateForm', this.multipleSelection)
      },
      getValueByKey(data,key) {
        let obj = {}
        obj = data.find(item => {
          return item.value == key
        })
        if(obj && obj.label){
          return obj.label
        }
      },
    }
  }
</script>

<style>
</style>
